$(function() {
  var listData = {
    "initData": { //默认参数配置
      "page": "1",
      "pageSize": "10"
    },
    "url": site.apiUrl + "/hospitalManager/selectMedicalHospitalInfoList", // 数据请求url
    "type": "get",
    // "checkname": "datachk",
    "col": [ //数据列
      { "field": "hospitalName", "display": "优惠券标题" },
      { "field": "doctorNum", "display": "优惠金额" },
      { "field": "doctorNum", "display": "使用条件" },
      { "field": "createTime", "display": "有效日期", "fun": "changeDate2", "width": "2" },
      {
        "field": "level",
        "display": "权重",
        "show": [
          { "value": "1", "html": "<span>1</span>" },
          { "value": "2", "html": "<span>2</span>" }
        ]
      },
      {
        "field": "status",
        "display": "状态",
        "show": [
          { "value": "0", "html": "<span class='red'>关闭</span>" },
          { "value": "1", "html": "<span class='green'>开启</span>" }
        ]
      },
      { "field": "doctorNum", "display": "当前次已发放张数" },
      { "field": "doctorNum", "display": "当前所属多少次,总共发放多少张" },
      { "field": "createTime", "display": "录入日期", "fun": "changeDate", "width": "2" }
    ],
    "option": [ // 操作按钮配置
      { "icon": "ogoto", "tip": "审查", "url": "view.html?id={{id}}" },
      { "icon": "edit", "tip": "编辑", "url": "edit.html?id={{id}}&type=2" },
      { "icon": "del", "tip": "删除", "fun": "deleteBrand('{{id}}')" }
    ]
  }
  $("#couponList").getList(listData);

  //批量删除
  $("#batchDelBtn").click(function() {
    var c = "";
    $("input[name=datachk]:checked").each(function() {
      c += $(this).val() + ',';
    });
    if (c != null && c != '') {
      deleteBrand(c);
    } else {
      ly.toast("请选择删除项");
    }
  });

});

// 设置默认图片
function changePic(v, o, d) {
  if (d.coverImg != null && d.coverImg != '') {
    $(o).html("<img src=" + d.coverImg + " width='30' height='30'>");
  } else {
    $(o).html("<img src='" + site.interUrl + "/img/default_head.jpg' width='30' height='30'>");
  }
}

// 时间戳转换时间
function changeDate(v, o, d) {
  var baseDateTime;
  var timestamp = new Date(d.createTime);
  baseDateTime = timestamp.toLocaleDateString().replace(/\//g, "-") + " " + timestamp.toTimeString().substr(0, 8)
  if (d.createTime != null && d.createTime != '') {
    $(o).html(baseDateTime)
  }
}
function changeDate2(v, o, d) {
  var baseDateTime;
  var timestamp = new Date(d.createTime);
  baseDateTime = timestamp.toLocaleDateString().replace(/\//g, "-") + " " + timestamp.toTimeString().substr(0, 8)
  if (d.createTime != null && d.createTime != '') {
    $(o).html(baseDateTime)
  }
}

// 跳转用户管理列表
function goUserPage(v, o, d) {
  $(o).html('<a href="'+ site.interUrl +'pages/doctorManage/list.html?groupid=2&id='+ d.id +'" title="">'+ d.doctorNum +'</a>');
}

//删除
function deleteBrand(id) {
  ly.confirm("确定删除吗？", function(r) {
    var url = site.apiUrl + "/hospitalManager/deleteMedicalPopular";
    request({
      url: url,
      data: { "id": id },
      type: "post",
      callback: function(f) {
        if (f.code == 200) {
          ly.toast("删除成功");
          $("#couponList").getList.refresh(); //刷新当前分页
        } else {
          ly.toast(f.desc || f.msg);
        }
      }
    })
  })
}